package com.foxchen.offline.task.controller;

import com.foxchen.offline.task.bean.ResponseBean;
import com.foxchen.offline.task.bean.TaskPojo;
import com.foxchen.offline.task.dto.TaskInfoDto;
import com.foxchen.offline.task.service.OfflineTaskService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RequestMapping("/offline-export")
@RestController
@Slf4j
@Tag(name = "离线任务相关")
public class OfflineTaskController {
    @Resource
    private OfflineTaskService offlineTaskService;

    @Operation(summary = "创建离线任务")
    @PostMapping("/create-task")
    public ResponseBean<String> createTask(@Valid @RequestBody TaskInfoDto taskInfo) {
        log.info("OfflineTaskController createTask:{}",taskInfo);
       return offlineTaskService.createTask(taskInfo);
    }

    @Operation(summary = "查询离线任务")
    @PostMapping("/list-task/{page}/{pageSize}")
    public ResponseBean<PageInfo<TaskPojo>> listTask(@RequestBody TaskInfoDto taskInfo,
                                                     @PathVariable("page") Integer page,
                                                     @PathVariable("pageSize") Integer pageSize) {
        log.info("OfflineTaskController listTask:{}",taskInfo);
        PageInfo<TaskPojo> taskPojoPageInfo = offlineTaskService.listTask(taskInfo, page, pageSize);
        return ResponseBean.success(taskPojoPageInfo);
    }

    @Operation(summary = "删除离线任务")
    @DeleteMapping("/delete-task/{id}")
    public ResponseBean<String> deleteTask(@PathVariable String id) {
        log.info("OfflineTaskController delete:{}",id);
        return offlineTaskService.deleteTask(id);
    }

    @Operation(summary = "下载离线任务文件")
    @GetMapping("/download-task/{id}")
    public void downloadTask(@PathVariable String id, HttpServletResponse response) throws Exception {
        log.info("OfflineTaskController downloadTask:{}",id);
        offlineTaskService.downloadTask(id,response);
    }
}
